<template>
	<view class="mainpadding">
		<view class="flexbetween ">
			<view :class="type==1?'yixz flexcenter':'weixianz flexcenter'" @click="selectab(1)">
				<view>个人认证</view>
			</view>
			<view :class="type==1?'weixianz flexcenter':'yixz flexcenter'" @click="selectab(2)">
				<view>企业认证</view>
			</view>
		</view>
		<!-- 个人认证 -->
		<view class="" v-if="type==1">
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">姓名</view>
				<input class="textright" placeholder="请输入姓名" type="text" v-model="name"/>
			</view>
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">手机号</view>
				<input class="textright" placeholder="请输入手机号" type="number" v-model="mobile"/>
			</view>
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">身份证号码</view>
				<input class="textright" placeholder="请输入身份证" type="text" v-model="idcard"/>
			</view>
			<view class="margin_top4 flexcenter" @click="uploadflag=1;uploadImg()">
				<image class="sfzcc" src="../static/image/system/sfzzm.png" v-if="!idcard_zimage_text" mode=""></image>
				<image class="sfzcc" :src="idcard_zimage_text" v-if="idcard_zimage_text" mode=""></image>
			</view>
			<view class="margin_top4 flexcenter" @click="uploadflag=2;uploadImg()">
				<image class="sfzcc" src="../static/image/system/sfzfm.png" v-if="!idcard_fimage_text" mode=""></image>
				<image class="sfzcc" :src="idcard_fimage_text" v-if="idcard_fimage_text" mode=""></image>
			</view>
			<view class="bigbtn margin_top5" @click="submit()" v-if="userInfo.smrz==null || userInfo.smrz.status==0">提交并支付认证费￥300</view>
			<view class="bigbtn margin_top5" @click="submit()" v-else>提交</view>
		</view>
		<!-- 企业认证 -->
		<view class="" v-if="type==2">
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">企业名称</view>
				<input class="textright" placeholder="请输入企业名称" type="text" v-model="campany_name"/>
			</view>
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">企业法人</view>
				<input class="textright" placeholder="请输入企业法人" type="text" v-model="name" />
			</view>
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">法人电话</view>
				<input class="textright" placeholder="请输入法人电话" type="number" v-model="mobile"/>
			</view>
			<view class="xiahuaxian flexbetween margin_top" @click="pickerdzshow = true">
				<view class="titletext xiaohei">地区</view>
				<input class="textright" placeholder="点击选择地区" type="text" disabled="" v-model="city_text" />
			</view>
			<view class="xiahuaxian flexbetween margin_top">
				<view class="titletext xiaohei">统一社会信用代码</view>
				<input class="textright" placeholder="请输入" type="text" v-model="idcard"/>
			</view>
			<view class="flexcenter" @click="uploadflag=3;uploadImg()">
				<view class="scbox flexcenter margin_top" v-if="!yyzz_image_text">
					<view class="">
						<view class="flexcenter">
							<u-icon name="plus" color="#B8B8B8" size="58"></u-icon>
						</view>
						<view class="sanshier margin_top textcenter" style="color: #B8B8B8;">上传营业执照</view>
					</view>
				</view>
				<view class="scbox margin_top" v-if="yyzz_image_text" >
					<image :src="yyzz_image_text" class="scbox" mode=""></image>
				</view>
			</view>
			<view class="bigbtn margin_top5" @click="submit()" v-if="userInfo.smrz==null || userInfo.smrz.status==0">提交并支付认证费￥300</view>
			<view class="bigbtn margin_top5" @click="submit()" v-else>提交</view>
		</view>
		<pickercity :messageInfo="{pickerdzshow:pickerdzshow}" @surecity="surecity"></pickercity>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				type: 1,//1=个人认证,2=企业认证
				campany_name:"",
				name:"",
				mobile:"",
				idcard:"",
				idcard_zimage:"",
				idcard_fimage:"",
				province_id:"",
				city_id:"",
				yyzz_image:"",
				idcard_zimage_text:"",
				idcard_fimage_text:"",
				yyzz_image_text:"",
				city_text:"",
				pickerdzshow:false,
				uploadflag:1,
				userInfo:{}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.userInfo = res.data
					let data = res.data.smrz
					this.type = data.type
					this.campany_name = data.campany_name
					this.name = data.name
					this.mobile = data.mobile
					this.idcard = data.idcard
					this.idcard_zimage = data.idcard_zimage
					this.idcard_zimage_text = data.idcard_zimage_text
					this.idcard_fimage = data.idcard_fimage
					this.idcard_fimage_text = data.idcard_fimage_text
					this.province_id = data.province_id
					this.city_id = data.city_id
					this.yyzz_image = data.yyzz_image
					this.city_text = data.cityinfo=="/"?"":data.cityinfo
				})
			},
			uploadImg(value) { //上传图片
			if (!httpRequest.checkIsLogin()) {
				uni.navigateTo({
					url: "/pages/login/login"
				})
				return false
			}
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(value, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i])
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(value, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i])
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					if (this.uploadflag == 1) {
						this.idcard_zimage = res.url
						this.idcard_zimage_text = url
					}
					if (this.uploadflag == 2) {
						this.idcard_fimage = res.url
						this.idcard_fimage_text = url
					}
					if (this.uploadflag == 3) {
						this.yyzz_image = res.url
						this.yyzz_image_text = url
					}
				}).catch(err => {});
			},
			surecity(e) {
				this.pickerdzshow = false
				if(e){
					this.city_text = e.provence + "/" + e.city
					this.province_id = e.provenceid
					this.city_id = e.cityid
				}
			},
			submit(){
				if(this.type==1){
					if(this.name==""){
						httpRequest.toast("请输入姓名")
						return false
					}
					if(this.mobile==""){
						httpRequest.toast("请输入手机号")
						return false
					}
					if(this.idcard==""){
						httpRequest.toast("请输入身份证号")
						return false
					}
					if(this.idcard_zimage==""){
						httpRequest.toast("请上传身份证正面照")
						return false
					}
					if(this.idcard_fimage==""){
						httpRequest.toast("请上传身份证反面照")
						return false
					}
				}
				if(this.type==2){
					if(this.campany_name==""){
						httpRequest.toast("请输入企业名称")
						return false
					}
					if(this.name==""){
						httpRequest.toast("请输入企业法人")
						return false
					}
					if(this.mobile==""){
						httpRequest.toast("请输入法人电话")
						return false
					}
					if(this.city_text==""){
						httpRequest.toast("请选择地区")
						return false
					}
					if(this.idcard==""){
						httpRequest.toast("请输入统一社会信用代码")
						return false
					}
					if(this.yyzz_image==""){
						httpRequest.toast("请上传营业执照")
						return false
					}
				}
				uni.showLoading({
					mask:true,
					title:"请稍后"
				})
				httpRequest.request('/api/user/smrzApply', 'GET', {
					type:this.type,
					campany_name:this.campany_name,
					name:this.name,
					mobile:this.mobile,
					idcard:this.idcard,
					idcard_zimage:this.idcard_zimage,
					idcard_fimage:this.idcard_fimage,
					province_id:this.province_id,
					city_id:this.city_id,
					yyzz_image:this.yyzz_image
				}).then(res => {
					if(res.code==1){
						if(this.userInfo.smrz==null || this.userInfo.smrz.status==0){
							this.pay()
						}else{
							uni.hideLoading()
							httpRequest.toast(res.msg)
							setTimeout(()=>{
								uni.navigateBack(1)
							},1000)
						}
					}else{
						uni.hideLoading()
						httpRequest.toast(res.msg)
					}
				})
				
			},
			pay(){
				httpRequest.request('/api/smrzorder/payment', 'POST', {
				}).then(res => {
					let data = res.data.wxconfig
					uni.hideLoading()
					if (res.code == 1) {
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: data.timeStamp,
							nonceStr: data.nonceStr,
							package: data.package,
							signType: data.signType,
							paySign: data.paySign,
							success: function(res) {
								// 支付成功
								uni.navigateBack(1)
							},
							fail: function(err) {
							}
						});
					}else{
						httpRequest.toast(res.msg)
					}
				})
			},
			// 跳转审核结果
			tzshjg(){
				uni.navigateTo({
					url:'/pages_mypage/shenhejg'
				})
			},
			selectab(i) {
				if (i == this.type) {
					return false
				}
				this.type = i
			},
		}
	}
</script>

<style lang="scss" scoped>
	.scbox {
		width: 500rpx;
		height: 542rpx;
		background: #F3F3F3;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}

	.sfzcc {
		width: 500rpx;
		height: 280rpx;
	}

	.weixianz {
		width: 50%;
		height: 64rpx;
		font-weight: 500;
		font-size: 24rpx;
		color: #222222;
		border: 1rpx solid #2C3034;
	}

	.yixz {
		width: 50%;
		height: 68rpx;
		background: #2C3034;
		font-weight: 500;
		font-size: 24rpx;
		color: #FFFFFF;
	}
</style>